package club.dataresources.controller;

import club.dataresources.bo.BarberBo;
import club.dataresources.bo.WaiterBo;
import club.dataresources.common.R;
import club.dataresources.entity.Barber;
import club.dataresources.entity.Waiter;
import club.dataresources.service.BarberService;
import club.dataresources.service.WaiterService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author
 * @create 2018-11-11 10:28
 **/
@RequestMapping(value = "/admin/member")
@Controller
public class AdminMemberController {
    @Autowired
    private BarberService barberService;
    
    @Autowired
    private WaiterService waiterService;
    
    @RequestMapping("/addBarber")
    public @ResponseBody
    R addBarber(
            //@RequestBody Barber barber,
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam("barberName") String barberName,
            @RequestParam("barberLevel") String barberLevel,
            @RequestParam("serviceCharge") float serviceCharge,
            @RequestParam("barberImg") String barberImg){
        System.out.println("================新增理发师================");
        try{
            Barber barber = new Barber();
            barber.setBarberName(barberName);
            barber.setBarberLevel(barberLevel);
            barber.setServiceCharge(serviceCharge);
            barber.setBarberImg(barberImg);
            if(barberService.addBarber(barber)){
                return R.ok().put("code",200).put("msg","显示完毕");
            }else {
                return R.error().put("code",500).put("msg","新增失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/updateBarber")
    public @ResponseBody
    R updateBarber(
            //@RequestBody Barber barber,
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam("barberName") String barberName,
            @RequestParam("barberLevel") String barberLevel,
            @RequestParam("serviceCharge") float serviceCharge,
            @RequestParam("barberId")Integer barberId){
        System.out.println("================修改理发师================");
        try{
            Barber barber = new Barber();
            barber.setBarberId(barberId);
            barber.setBarberName(barberName);
            barber.setBarberLevel(barberLevel);
            barber.setServiceCharge(serviceCharge);
            if(barberService.updateBarber(barber)){
                return R.ok().put("code",200).put("msg","显示完毕");
            }else {
                return R.error().put("code",500).put("msg","修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/removeBarber")
    public @ResponseBody
    R removeBarber(@RequestParam("barberId") Integer barberId){
        System.out.println("================删除理发师================");
        try{
            if(barberService.removeBarber(barberId)){
                return R.ok().put("code",200).put("msg","显示完毕");
            }else {
                return R.error().put("code",500).put("msg","删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/listBarber")
    public @ResponseBody
    R listBarber(
            HttpServletRequest request,
            HttpServletResponse response){
        System.out.println("================查询显示理发师================");
        try{
            BarberBo barberBo = new BarberBo();
            barberBo.setPage(1);
            barberBo.setRows(10);
            Page<Barber> liPage = barberService.listBarber(barberBo.getPage(),barberBo.getRows(),barberBo);
            return R.ok().put("data",liPage.getResult()).put("code",200).put("msg","显示完毕").put("total",liPage.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    /*@RequestMapping("/listBarber")
    public @ResponseBody
    R listBarber(@RequestBody BarberBo barberBo){
        System.out.println("================查询显示理发师================");
        try{
            Page<Barber> liPage = barberService.listBarber(barberBo.getPage(),barberBo.getRows(),barberBo);
            return R.ok().put("data",liPage.getResult()).put("code",200).put("msg","显示完毕").put("total",liPage.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }*/

    @RequestMapping("/addWaiter")
    public @ResponseBody
    R addWaiter(@RequestBody Waiter waiter){
        System.out.println("================新增服务员================");
        try{
            if(waiterService.addWaiter(waiter)){
                return R.ok().put("code",200).put("msg","显示完毕");
            }else {
                return R.error().put("code",500).put("msg","新增失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/updateWaiter")
    public @ResponseBody
    R updateWaiter(@RequestBody Waiter waiter){
        System.out.println("================修改服务员================");
        try{
            if(waiterService.updateWaiter(waiter)){
                return R.ok().put("code",200).put("msg","显示完毕");
            }else {
                return R.error().put("code",500).put("msg","修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/removeWaiter")
    public @ResponseBody
    R removeWaiter(@RequestParam("waiterId") Integer waiterId){
        System.out.println("================删除服务员================");
        try{
            if(waiterService.removeWaiter(waiterId)){
                return R.ok().put("code",200).put("msg","显示完毕");
            }else {
                return R.error().put("code",500).put("msg","删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    @RequestMapping("/listWaiter")
    public @ResponseBody
    R listWaiter(@RequestBody WaiterBo waiterBo){
        System.out.println("================查询显示服务员================");
        try{
            Page<Waiter> liPage = waiterService.listWaiter(waiterBo.getPage(),waiterBo.getRows(),waiterBo);
            return R.ok().put("data",liPage.getResult()).put("code",200).put("msg","显示完毕").put("total",liPage.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }
}
